package xyz.yuanjilu.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xyz.yuanjilu.entity.CommunityEntity;
import xyz.yuanjilu.service.CommunityService;
import xyz.yuanjilu.utils.PageUtils;
import xyz.yuanjilu.utils.Query;
import xyz.yuanjilu.utils.R;


/**
 * 社区信息
 * 
 * @author 空空儿雪丽
 * @email KKOMGE@QQ.COM
 * @date 2017-08-28 20:35:03
 */
@RestController
@RequestMapping("community")
public class CommunityController {
	@Autowired
	private CommunityService communityService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("community:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<CommunityEntity> communityList = communityService.queryList(query);
		int total = communityService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(communityList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("community:info")
	public R info(@PathVariable("id") Long id){
		CommunityEntity community = communityService.queryObject(id);
		
		return R.ok().put("community", community);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("community:save")
	public R save(@RequestBody CommunityEntity community){
		communityService.save(community);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("community:update")
	public R update(@RequestBody CommunityEntity community){
		communityService.update(community);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("community:delete")
	public R delete(@RequestBody Long[] ids){
		communityService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
